package com.linran.utils.lock.controller;

import com.linran.utils.lock.anotation.DistributionLock;
import com.linran.utils.lock.constant.LockTryType;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 加锁演示
 *
 * @author lin
 * @date 2020-07-13 15:31
 */
@RestController
@RequestMapping("lock")
public class LockSampleController {

    /**
     * 引用类型-字符串
     * <p>
     * 加锁结果 LIN:RAN:LOCK:lock:get:123456 ttl:30s
     *
     * @param uuid 加锁key
     */
    @GetMapping("/{uuid}")
    @DistributionLock(modules = {"lock", "get"}, leaseTime = 30)
    public String getString(@PathVariable String uuid) {
        System.out.println("getString :" + uuid);
        return "getString :" + uuid;
    }

    /**
     * 引用类型-Integer
     * <p>
     * 加锁结果 LIN:RAN:LOCK:lock:get:integer:56 ttl:5m
     *
     * @return
     */
    @GetMapping("/integer")
    @DistributionLock(modules = {"lock", "get", "integer"}, leaseTime = 5, timeUnit = TimeUnit.MINUTES)
    public String getInteger(@RequestParam("id") Integer id) {
        System.out.println("getInteger :" + id);
        return "getInteger :" + id;
    }

    /**
     * 基本类型-int
     *
     * @param id
     * @return
     */
    @GetMapping("/int")
    @DistributionLock(modules = {"lock", "get", "int"}, leaseTime = 300, lockTryType = LockTryType.TRY, waitTime = 10)
    public String getInt(@RequestParam("id") int id) {
        System.out.println("getInt :" + id);
        return "getInt :" + id;
    }
}

class Param{
    private String strId;
}
